1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.invoke.anon;
27
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.util.Arrays;
31 import java.util.HashSet;
32 import java.util.IdentityHashMap;
33 import java.util.Map;
34
35 import static sun.invoke.anon.ConstantPoolVisitor.*;
36
37
38
39
40
41
42
43
44
45
46
47
48 public class ConstantPoolPatch {
49 final ConstantPoolParser outer;
50 final Object[] patchArray;
51
52 ConstantPoolPatch(ConstantPoolParser outer) {
53 this.outer = outer;
54 this.patchArray = new Object[outer.getLength()];
55 }
56
57
58
59
60
61
62
63
64 public ConstantPoolPatch(byte[] classFile) throws InvalidConstantPoolFormatException {
65 this(new ConstantPoolParser(classFile));
66 }
67
68
69
70
71
72
73
74
75 public ConstantPoolPatch(Class<?> templateClass) throws IOException, InvalidConstantPoolFormatException {
76 this(new ConstantPoolParser(templateClass));
77 }
78
79
80
81
82
83
84
85
86 public ConstantPoolPatch(ConstantPoolPatch patch) {
87 outer = patch.outer;
88 patchArray = patch.patchArray.clone();
89 }
90
91
92 public ConstantPoolParser getParser() {
93 return outer;
94 }
95
96
97 public byte getTag(int index) {
98 return outer.getTag(index);
99 }
100
101
102
103
104
105
106 public Object getPatch(int index) {
107 Object value = patchArray[index];
108 if (value == null) return null;
109 switch (getTag(index)) {
110 case CONSTANT_Fieldref:
111 case CONSTANT_Methodref:
112 case CONSTANT_InterfaceMethodref:
113 if (value instanceof String)
114 value = stripSemis(2, (String) value);
115 break;
116 case CONSTANT_NameAndType:
117 if (value instanceof String)
118 value = stripSemis(1, (String) value);
119 break;
120 }
121 return value;
122 }
123
124
125 public void clear() {
126 Arrays.fill(patchArray, null);
127 }
128
129
130 public void clear(int index) {
131 patchArray[index] = null;
132 }
133
134
135 public Object[] getPatches() {
136 return patchArray.clone();
137 }
138
139
140 public Object[] getOriginalCP() throws InvalidConstantPoolFormatException {
141 return getOriginalCP(0, patchArray.length, -1);
142 }
143
144
145
146
147
148
149
150
151 public void putPatches(final Map<String,String> utf8Map,
152 final Map<String,Object> classMap,
153 final Map<Object,Object> valueMap,
154 boolean deleteUsedEntries) throws InvalidConstantPoolFormatException {
155 final HashSet<String> usedUtf8Keys;
156 final HashSet<String> usedClassKeys;
157 final HashSet<Object> usedValueKeys;
158 if (deleteUsedEntries) {
159 usedUtf8Keys = (utf8Map == null) ? null : new HashSet<String>();
160 usedClassKeys = (classMap == null) ? null : new HashSet<String>();
161 usedValueKeys = (valueMap == null) ? null : new HashSet<Object>();
162 } else {
163 usedUtf8Keys = null;
164 usedClassKeys = null;
165 usedValueKeys = null;
166 }
167
168 outer.parse(new ConstantPoolVisitor() {
169
170 @Override
171 public void visitUTF8(int index, byte tag, String utf8) {
172 putUTF8(index, utf8Map.get(utf8));
173 if (usedUtf8Keys != null) usedUtf8Keys.add(utf8);
174 }
175
176 @Override
177 public void visitConstantValue(int index, byte tag, Object value) {
178 putConstantValue(index, tag, valueMap.get(value));
179 if (usedValueKeys != null) usedValueKeys.add(value);
180 }
181
182 @Override
183 public void visitConstantString(int index, byte tag, String name, int nameIndex) {
184 if (tag == CONSTANT_Class) {
185 putConstantValue(index, tag, classMap.get(name));
186 if (usedClassKeys != null) usedClassKeys.add(name);
187 } else {
188 assert(tag == CONSTANT_String);
189 visitConstantValue(index, tag, name);
190 }
191 }
192 });
193 if (usedUtf8Keys != null) utf8Map.keySet().removeAll(usedUtf8Keys);
194 if (usedClassKeys != null) classMap.keySet().removeAll(usedClassKeys);
195 if (usedValueKeys != null) valueMap.keySet().removeAll(usedValueKeys);
196 }
197
198 Object[] getOriginalCP(final int startIndex,
199 final int endIndex,
200 final int tagMask) throws InvalidConstantPoolFormatException {
201 final Object[] cpArray = new Object[endIndex - startIndex];
202 outer.parse(new ConstantPoolVisitor() {
203
204 void show(int index, byte tag, Object value) {
205 if (index < startIndex || index >= endIndex) return;
206 if (((1 << tag) & tagMask) == 0) return;
207 cpArray[index - startIndex] = value;
208 }
209
210 @Override
211 public void visitUTF8(int index, byte tag, String utf8) {
212 show(index, tag, utf8);
213 }
214
215 @Override
216 public void visitConstantValue(int index, byte tag, Object value) {
217 assert(tag != CONSTANT_String);
218 show(index, tag, value);
219 }
220
221 @Override
222 public void visitConstantString(int index, byte tag,
223 String value, int j) {
224 show(index, tag, value);
225 }
226
227 @Override
228 public void visitMemberRef(int index, byte tag,
229 String className, String memberName,
230 String signature,
231 int j, int k) {
232 show(index, tag, new String[]{ className, memberName, signature });
233 }
234
235 @Override
236 public void visitDescriptor(int index, byte tag,
237 String memberName, String signature,
238 int j, int k) {
239 show(index, tag, new String[]{ memberName, signature });
240 }
241 });
242 return cpArray;
243 }
244
245
246
247
248 void writeHead(OutputStream out) throws IOException {
249 outer.writePatchedHead(out, patchArray);
250 }
251
252
253
254
255 void writeTail(OutputStream out) throws IOException {
256 outer.writeTail(out);
257 }
258
259 private void checkConstantTag(byte tag, Object value) {
260 if (value == null)
261 throw new IllegalArgumentException(
262 "invalid null constant value");
263 if (classForTag(tag) != value.getClass())
264 throw new IllegalArgumentException(
265 "invalid constant value"
266 + (tag == CONSTANT_None ? ""
267 : " for tag "+tagName(tag))
268 + " of class "+value.getClass());
269 }
270
271 private void checkTag(int index, byte putTag) {
272 byte tag = outer.tags[index];
273 if (tag != putTag)
274 throw new IllegalArgumentException(
275 "invalid put operation"
276 + " for " + tagName(putTag)
277 + " at index " + index + " found " + tagName(tag));
278 }
279
280 private void checkTagMask(int index, int tagBitMask) {
281 byte tag = outer.tags[index];
282 int tagBit = ((tag & 0x1F) == tag) ? (1 << tag) : 0;
283 if ((tagBit & tagBitMask) == 0)
284 throw new IllegalArgumentException(
285 "invalid put operation"
286 + " at index " + index + " found " + tagName(tag));
287 }
288
289 private static void checkMemberName(String memberName) {
290 if (memberName.indexOf(';') >= 0)
291 throw new IllegalArgumentException("memberName " + memberName + " contains a ';'");
292 }
293
294
295
296
297
298
299
300
301
302
303 public void putUTF8(int index, String utf8) {
304 if (utf8 == null) { clear(index); return; }
305 checkTag(index, CONSTANT_Utf8);
306 patchArray[index] = utf8;
307 }
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329 public void putConstantValue(int index, Object value) {
330 if (value == null) { clear(index); return; }
331 byte tag = tagForConstant(value.getClass());
332 checkConstantTag(tag, value);
333 checkTag(index, tag);
334 patchArray[index] = value;
335 }
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 public void putConstantValue(int index, byte tag, Object value) {
358 if (value == null) { clear(index); return; }
359 checkTag(index, tag);
360 if (tag == CONSTANT_Class && value instanceof String) {
361 checkClassName((String) value);
362 } else if (tag == CONSTANT_String) {
363
364 } else {
365
366 checkConstantTag(tag, value);
367 }
368 checkTag(index, tag);
369 patchArray[index] = value;
370 }
371
372
373
374
375
376
377
378
379
380
381
382
383 public void putDescriptor(int index, String memberName, String signature) {
384 checkTag(index, CONSTANT_NameAndType);
385 checkMemberName(memberName);
386 patchArray[index] = addSemis(memberName, signature);
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403 public void putMemberRef(int index, byte tag,
404 String className, String memberName, String signature) {
405 checkTagMask(tag, CONSTANT_MemberRef_MASK);
406 checkTag(index, tag);
407 checkClassName(className);
408 checkMemberName(memberName);
409 if (signature.startsWith("(") == (tag == CONSTANT_Fieldref))
410 throw new IllegalArgumentException("bad signature: "+signature);
411 patchArray[index] = addSemis(className, memberName, signature);
412 }
413
414 static private final int CONSTANT_MemberRef_MASK =
415 CONSTANT_Fieldref
416 | CONSTANT_Methodref
417 | CONSTANT_InterfaceMethodref;
418
419 private static final Map<Class<?>, Byte> CONSTANT_VALUE_CLASS_TAG
420 = new IdentityHashMap<Class<?>, Byte>();
421 private static final Class[] CONSTANT_VALUE_CLASS = new Class[16];
422 static {
423 Object[][] values = {
424 {Integer.class, CONSTANT_Integer},
425 {Long.class, CONSTANT_Long},
426 {Float.class, CONSTANT_Float},
427 {Double.class, CONSTANT_Double},
428 {String.class, CONSTANT_String},
429 {Class.class, CONSTANT_Class}
430 };
431 for (Object[] value : values) {
432 Class<?> cls = (Class<?>)value[0];
433 Byte tag = (Byte) value[1];
434 CONSTANT_VALUE_CLASS_TAG.put(cls, tag);
435 CONSTANT_VALUE_CLASS[(byte)tag] = cls;
436 }
437 }
438
439 static Class<?> classForTag(byte tag) {
440 if ((tag & 0xFF) >= CONSTANT_VALUE_CLASS.length)
441 return null;
442 return CONSTANT_VALUE_CLASS[tag];
443 }
444
445 static byte tagForConstant(Class<?> cls) {
446 Byte tag = CONSTANT_VALUE_CLASS_TAG.get(cls);
447 return (tag == null) ? CONSTANT_None : (byte)tag;
448 }
449
450 private static void checkClassName(String className) {
451 if (className.indexOf('/') >= 0 || className.indexOf(';') >= 0)
452 throw new IllegalArgumentException("invalid class name " + className);
453 }
454
455 static String addSemis(String name, String... names) {
456 StringBuilder buf = new StringBuilder(name.length() * 5);
457 buf.append(name);
458 for (String name2 : names) {
459 buf.append(';').append(name2);
460 }
461 String res = buf.toString();
462 assert(stripSemis(names.length, res)[0].equals(name));
463 assert(stripSemis(names.length, res)[1].equals(names[0]));
464 assert(names.length == 1 ||
465 stripSemis(names.length, res)[2].equals(names[1]));
466 return res;
467 }
468
469 static String[] stripSemis(int count, String string) {
470 String[] res = new String[count+1];
471 int pos = 0;
472 for (int i = 0; i < count; i++) {
473 int pos2 = string.indexOf(';', pos);
474 if (pos2 < 0) pos2 = string.length();
475 res[i] = string.substring(pos, pos2);
476 pos = pos2;
477 }
478 res[count] = string.substring(pos);
479 return res;
480 }
481
482 public String toString() {
483 StringBuilder buf = new StringBuilder(this.getClass().getName());
484 buf.append("{");
485 Object[] origCP = null;
486 for (int i = 0; i < patchArray.length; i++) {
487 if (patchArray[i] == null) continue;
488 if (origCP != null) {
489 buf.append(", ");
490 } else {
491 try {
492 origCP = getOriginalCP();
493 } catch (InvalidConstantPoolFormatException ee) {
494 origCP = new Object[0];
495 }
496 }
497 Object orig = (i < origCP.length) ? origCP[i] : "?";
498 buf.append(orig).append("=").append(patchArray[i]);
499 }
500 buf.append("}");
501 return buf.toString();
502 }
503 }